package com.bwie.service.imp;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.Es.EsGoods;
import com.bwie.mapper.GoodsMapper;
import com.bwie.pojo.Goods;
import com.bwie.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Override
    public Page<Goods> pageList(Page<Goods> goodsPage) {
        return goodsMapper.pageList(goodsPage);
    }

    @Override
    public List<Goods> list1() {
        return goodsMapper.list1();
    }

    @Override
    public void createEs() {
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(EsGoods.class);
        boolean exists = indexOperations.exists();
        if (exists){
            indexOperations.delete();
        }
        indexOperations.create();
        Document mapping = indexOperations.createMapping();
        indexOperations.putMapping(mapping);
    }
}
